package com.chaopei.day07;

import javax.imageio.stream.ImageInputStream;
import java.io.*;

/**
 * @Author Corday 天涯
 * @Date 2022/3/11 9:45
 * Java的io流用于管理数据的输入与输出，简单地说流是一种动态数据
 * ,Java通过i/o工具来管理动态数据，实现数据的输入输出
 * 1.流的分类
 * 按流的方向可以分为输入流与输出流
 * 按流的类型可以分为,字节流与字符流
 * 归纳为:字节输入流与字节输出流,字符输入流与字符输出流
 */
public class MyIO {
    //    1.输入流的应用
//    1.1字节输入流,从磁盘或网络上读入数据到程序中
//    InputStream/FileInputStream/BufferedInputStream/ObjectInputStream
//    InputStreamReader/DataInputStream/ImageInputStream
    public static void playInputStream() {
        //(1)创建输入字节流对象,打开文件
        InputStream in = null;
        //增强程序功能
        BufferedInputStream bis = null;
        //字符输入汉 BufferedReader br = null;
        try {
            in = new FileInputStream("C:/ChaoPei/JavaBase/day07/mynote.txt");
            //创建缓冲字节流对象,能设置每次读入的字节数
            bis = new BufferedInputStream(in);
            ////创建字符输入流对象
//            br = new BufferedReader(new InputStreamReader(bis));
//            String flag = br.readLine();
//            String str = "";
//            while (flag != null) {
//                str += flag+"\n";
//                flag = br.readLine();
//            }

            //(2)读取文件中的数据
            //一次获取文件的有效字节数
            int size = in.available();
            //将文件中的内容读入到缓存中
            byte[] buffer = new byte[size];
            //将字节数组中的数据读取程序中
            String str = "";
            while (bis.read(buffer) != -1) {
                str += new String(buffer, 0, buffer.length);
                str += new String(buffer);
            }
            //(3)关闭流
            bis.close();
            in.close();
            System.out.println(str);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }//instr

    //    对象流入流
    public static void playInputObject() {
        //请完成集合对象与数组对象的读写
        InputStream in = null;
        BufferedInputStream bis = null;
        ObjectInputStream ois = null;
        try {
            in = new FileInputStream("c:/temp/obj.txt");
            bis = new BufferedInputStream(in);
            ois = new ObjectInputStream(bis);
            User temp = (User) ois.readObject();
            while (temp != null) {
                System.out.println(temp.getUsernmae() + ":" + temp.getGender());
                temp = (User) ois.readObject();
            }
            //关闭输入流
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //1.2 字符输入流
    public static void playInputChar() {
        //(1)声明并创建字符输入流
        Reader reader = null;
        BufferedReader br = null;
        try {
            reader = new FileReader("C:/ChaoPei/JavaBase/day07/mynote.txt");
            br = new BufferedReader(reader);
            //char[] buffer = new char[1024];
            //br.read(buffer);
            StringBuilder sbd = new StringBuilder();
            String temp = br.readLine();
            while (temp != null) {
                sbd.append(temp + "\n");
                temp = br.readLine();
            }
            br.close();
            reader.close();
            //sbd.toString();
            System.out.println(sbd.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //  2.输出流,将程序中的数据输出到磁盘或网络上
    public static void playOutputStream() {
        //2.1 字节输出流
        String[] str = {"床前明月光,\n", "疑是地上霜.\n", "举头望明月,\n", "低头思故乡.\n"};
        //分步写入50w字节
        String temp = "I am dog abc yhc Tree lorm pole pojo";
        //(1)创建输出流对象
        OutputStream out = null;
        BufferedOutputStream bos = null;
        try {
            //两个参数(文件名,是否为追加方式,默认为false表示覆盖)
            out = new FileOutputStream("poem.txt", true);
            //能够设置每次缓冲的字节数
            bos = new BufferedOutputStream(out, 4);
            //(2)写入到磁盘文件中
            //byte[] buffer = new byte[1024];
//            for (String s:str){
//                bos.write(s.getBytes());
//            }
            bos.write(temp.getBytes());
            //(3)关闭流
            bos.close();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //输出对象到磁盘文件中
    public static void outputObject() {
        //声明对象
        OutputStream os = null;
        BufferedOutputStream bos = null;
        ObjectOutputStream oos = null;
        //创建对象
        try {
            os = new FileOutputStream("c:/temp/obj.txt");
            bos = new BufferedOutputStream(os);
            //生成对象输出流对象
            oos = new ObjectOutputStream(bos);
            User user = new User("tom", "男");
            User u = new User("bean", "男");
            oos.writeObject(user);
            oos.writeObject(u);
            //写入一个null对象,防止读取时异常
            oos.writeObject(null);
            oos.close();
            bos.close();
            os.close();
            //请完成集合对象与数组对象的读写
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 请完成字符输出流，保存文本信息到磁盘文件中
//  Writer/FileWriter/BufferedWriter
    public static void playOutputWriter() {

    }

}

// 测试一下
class MyTest {
    public static void main(String[] args) {
//        MyIO.playInputStream();
//        MyIO.playOutputStream();
//        MyIO.outputObject();
//        MyIO.playInputObject();
//        MyIO.playInputChar();
    }
}
